import {
  ValidationArguments,
  ValidationOptions,
  ValidatorConstraint,
  ValidatorConstraintInterface,
  registerDecorator,
} from 'class-validator';

@ValidatorConstraint({ name: 'isPhoneNumber', async: false })
export class PasswordValidate implements ValidatorConstraintInterface {
  validate(
    value: string,
    args?: ValidationArguments,
  ): boolean | Promise<boolean> {
    const regex = /^1[3456789]\d{9}$/;
    return regex.test(value);
  }
  defaultMessage(validationArguments?: ValidationArguments): string {
    return `手机号码格式不正确`;
  }
}
export function IsPhoneNumber(validationOptions?: ValidationOptions) {
  return (object: Record<string, any>, propertyName: string) => {
    registerDecorator({
      target: object.constructor,
      propertyName,
      options: validationOptions,
      constraints: [],
      validator: PasswordValidate,
    });
  };
}
